package de.koller.worlders.engine.entity.mo;

import de.koller.worlders.engine.entity.mo.phsics.PhysicsMO;
import de.koller.worlders.engine.entity.plattform.Plattform;
import de.koller.worlders.engine.tiles.Tile;

public class Elevator extends PhysicsMO {
	
	private long nextMove = 0;
	private boolean waiting = false;
	private final long waitTime = 2000;
	private final double elevatorEnergie = 0.002;
	private final int spawnX, spawnY;
	private final boolean vertical;
	
	public Elevator( float x, float y, float w, float h, int color, boolean vertical ) {
		super( x, y, w, h );
		spawnX = (int)Math.floor( x );
		spawnY = (int)Math.floor( y );
		this.vertical = vertical;
	}

	@Override
	protected void init() {
		super.init();
		
		this.plattformHandler.setResetXenergie( false );
		
		//this.getForce().set( 0, 0 );
		this.mostate.setGravityAffected( false );
		if( vertical )
			this.getEnergy().setY( elevatorEnergie );
		else
			this.getEnergy().setX( elevatorEnergie );
		
		if( this.getMoTile() != null )
			((Plattform)this.getMoTile().getTileEntity()).addEffect( new ElevatorEffect() );
//		this.wroot.getEntityContainer().getElevators().add( this );
	}

	public void relocate( float x, float y ) {
		this.getPosition().set( x, y );
		//this.getForce().set( 0, 0 );
		this.mostate.setGravityAffected( false );
		
		if( vertical )
			this.getEnergy().setY( elevatorEnergie );
		else
			this.getEnergy().setX( elevatorEnergie );
		
		waiting = false;
	}
	
	@Override
	protected void release() {
		super.release();

//		this.wroot.getEntityContainer().getElevators().remove( this );
	}
	
	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		//this.getForce().set( 0, 0 );
		this.mostate.setGravityAffected( false );
		
		Tile to = wroot.getTileMap().getTileAt( 
				this.getPosition().getXf() + 0.1f - (!vertical ? 1.2f : 0), 
				this.getPosition().getYf() + 0.1f - ( vertical ? 1.2f : 0) ).getTile();
		if( to == null )
			return;
		
		if( to.getTileEntity() != null && to.getTileEntity() instanceof Plattform ) {
			if( !waiting ) {
				this.getEnergy().setY( 0 );
				this.getEnergy().setX( 0 );
				nextMove = wroot.getTime() + waitTime;
				waiting = true;
			} else if( nextMove < wroot.getTime() ) {
				waiting = false;
				
				if( vertical )
					this.getEnergy().setY( elevatorEnergie );
				else
					this.getEnergy().setX( elevatorEnergie );
			}
			
			return;
		}
		
		Tile tu = null;
		
		if( vertical )
			tu = wroot.getTileMap().getTileAt( 
					this.getPosition().getXf() + 0.1f, 
					this.getPosition().getYf() + this.getSize().getYf() + 1.2f ).getTile();
		else
			tu = wroot.getTileMap().getTileAt( 
					this.getPosition().getXf() + this.getSize().getXf() + 1.2f , 
					this.getPosition().getYf() + 0.1f ).getTile();
		
		if( tu.getTileEntity() != null && tu.getTileEntity() instanceof Plattform ) {
			if( !waiting ) {
				this.getEnergy().setY( 0 );
				this.getEnergy().setX( 0 );
				nextMove = wroot.getTime() + waitTime;
				waiting = true;
			} else if( nextMove < wroot.getTime() ) {
				waiting = false;
				
				if( vertical )
					this.getEnergy().setY( -elevatorEnergie );
				else
					this.getEnergy().setX( -elevatorEnergie );
			}
		}
		
	}

	public float getSpawnX() {
		return spawnX;
	}

	public float getSpawnY() {
		return spawnY;
	}

}
